<?php
/**
 * Created by DRCOM.
 * User: GZX
 * Datetime: 2021-07-27 16:38
 */

namespace app\api\controller;

use think\Db;

class Portal
{
    public function index()
    {
        $hour = date('H');
        $min = date('H:i');
        // $hour = time();
        $activeId = config('site.activity');

//        $ids = db('publisherInfo')->column('relation_id');
//        $rand = array_rand($ids);
        //获取随机的渠道id
//        $relationId = $ids[$rand];
        $relationId = db('publisherInfo')->where('id',date('d'))->value('relation_id');
//        halt($relationId);
        $portal = Db::table('tbl_portal')->where('flag', 1)
            ->order('release_time')
            ->find();

        $id = $portal['id'];
        $ip = $portal['ip'];
        $port = $portal['port'];

        $this->wlog($relationId);
        if(cache('tpwd')){
            $tpwd=cache('tpwd');
        }else{
            $this->wlog('get_activity_info');
             if($hour<0){
                $tpwd = $this->get_activity_info($activeId,$ip);
             }else{
                 $tpwd = get_activity_info($activeId,$relationId,$ip);
             }
             dump($tpwd);
            $tpwd = str_replace('/:/','',$tpwd);
            
            $this->wlog($tpwd);
        }

        dump($tpwd);

        if ($tpwd) {
            $tpwd = ($relationId . str_replace('￥', '$', $tpwd));
            echo $tpwd;
//            $res = $this->release($tpwd,$ip,$port);
//            $this->wlog($res);

            if ($min == '08:00' || $min == '12:00' || $min == '21:10'){
                db('wechatTmplmsg')->insert(['title' => 'portal_task', 'content' => $tpwd]);
            }
        }
//        Db::table('tbl_portal')->where('id', $id)
//        ->setField('release_time',date('Y-m-d H:i:s'));
//        if ($res == '[0]'){
//            echo "成功";
//        }

    }
    
    public function get_activity_info($content = 20150318020002597,$remark='')
{
    if ($content == '') $content = 20150318020002597;
    $data = [
        'apkey'                => '6a9ffd88-17a3-9f54-0bb7-b91a81405862',
        'tbname'               => 'tb05159005487',
        'pid'                  => 'mm_3307787517_2726950248_114493300225',//常规
        'relation_id'           => '2900106203',
        'activity_material_id' => $content,
        'hastpwd'              => 1,
        'tpwd_title'           => '一个好商品',
    ];

    
    $url = "http://api.web.ecapi.cn/taoke/getTbkActivityInfo?";
//    dump($data);
    $params = http_build_query($data);
    $result = file_get_contents($url . $params);
    $content = json_decode($result, true);
//    dump($content);
//    return $content;
//    trace($content, 'error');
    if (!empty($content['data'])) {
        db('tpwdReport')->insert(['pid' => $data['pid'], 'tpwd' => $content['data']['tpwd'], 'remark' => $remark]);
        return $content['data']['tpwd'];
    }
}

    //发布方案
    private function release($value, $ip = '124.16.4.5', $port = 9801)
    {
        $this->wlog(str_repeat('-', 32));
        $url = "http://$ip:$port/eportal/tmp/123fead50246387983ee340507115ef4.php?tpwd=".str_replace(' ','%20',$value);
        $this->wlog($url);
        $opts = ["http" => ["method" => "GET", "timeout" => 10]];
        $context = stream_context_create($opts);
        return @file_get_contents($url, false, $context);
    }

    private function wlog($msg)
    {
        $msg = date('Y-m-d H:i:s') . "\t" . $msg . PHP_EOL;
        echo $msg;
        file_put_contents('/tmp/release', $msg, FILE_APPEND);

    }

}